/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.suli.agvStation.service.impl;

import me.zhengjie.suli.agvStation.domain.AgvStation;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import me.zhengjie.suli.agvStation.repository.AgvStationRepository;
import me.zhengjie.suli.agvStation.service.AgvStationService;
import me.zhengjie.suli.agvStation.service.dto.AgvStationDto;
import me.zhengjie.suli.agvStation.service.dto.AgvStationQueryCriteria;
import me.zhengjie.suli.agvStation.service.mapstruct.AgvStationMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import cn.hutool.core.util.IdUtil;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @website https://eladmin.vip
* @description 服务实现
* @author WYH
* @date 2024-03-14
**/
@Service
@RequiredArgsConstructor
public class AgvStationServiceImpl implements AgvStationService {

    private final AgvStationRepository agvStationRepository;
    private final AgvStationMapper agvStationMapper;

    @Override
    public Map<String,Object> queryAll(AgvStationQueryCriteria criteria, Pageable pageable){
        Page<AgvStation> page = agvStationRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(agvStationMapper::toDto));
    }

    @Override
    public List<AgvStationDto> queryAll(AgvStationQueryCriteria criteria){
        return agvStationMapper.toDto(agvStationRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public AgvStationDto findById(String id) {
        AgvStation agvStation = agvStationRepository.findById(id).orElseGet(AgvStation::new);
        ValidationUtil.isNull(agvStation.getId(),"AgvStation","id",id);
        return agvStationMapper.toDto(agvStation);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public AgvStationDto create(AgvStation resources) {
        resources.setId(IdUtil.simpleUUID()); 
        return agvStationMapper.toDto(agvStationRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(AgvStation resources) {
        AgvStation agvStation = agvStationRepository.findById(resources.getId()).orElseGet(AgvStation::new);
        ValidationUtil.isNull( agvStation.getId(),"AgvStation","id",resources.getId());
        agvStation.copy(resources);
        agvStationRepository.save(agvStation);
    }

    @Override
    public void deleteAll(String[] ids) {
        for (String id : ids) {
            agvStationRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<AgvStationDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (AgvStationDto agvStation : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("工作站编码", agvStation.getStationCode());
            map.put("工作站状态", agvStation.getStatus());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}